package com.rbac.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rbac.admin.entity.SysPermission;
import com.rbac.admin.mapper.SysPermissionMapper;
import com.rbac.admin.service.CacheService;
import com.rbac.admin.service.SysPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 权限服务实现类
 */
@Service
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysPermission> implements SysPermissionService {

    @Autowired
    private CacheService cacheService;
    
    @Value("${cache.expiration.permission}")
    private long permissionCacheExpiration;
    
    // 缓存key前缀
    private static final String PERMISSION_ALL_CACHE_KEY = "permission:all";
    private static final String PERMISSION_USER_CACHE_KEY = "permission:user:";

    @Override
    public Page<SysPermission> listPermissionPage(Integer pageNum, Integer pageSize, String keyword) {
        Page<SysPermission> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SysPermission> queryWrapper = new LambdaQueryWrapper<>();
        
        // 添加查询条件
        if (StringUtils.hasText(keyword)) {
            queryWrapper.like(SysPermission::getPermissionName, keyword)
                    .or().like(SysPermission::getPermissionCode, keyword)
                    .or().like(SysPermission::getPermissionType, keyword);
        }
        
        // 排序
        queryWrapper.orderByAsc(SysPermission::getSortOrder);
        
        return page(page, queryWrapper);
    }

    @Override
    public List<SysPermission> listAllPermissions() {
        // 尝试从缓存获取
        List<SysPermission> permissionList = cacheService.get(PERMISSION_ALL_CACHE_KEY);
        if (permissionList != null) {
            return permissionList;
        }
        
        // 缓存未命中，查询数据库
        LambdaQueryWrapper<SysPermission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(SysPermission::getSortOrder);
        permissionList = list(queryWrapper);
        
        // 存入缓存
        cacheService.set(PERMISSION_ALL_CACHE_KEY, permissionList, permissionCacheExpiration);
        
        return permissionList;
    }

    @Override
    public List<SysPermission> getPermissionsByUserId(Long userId) {
        // 尝试从缓存获取
        String cacheKey = PERMISSION_USER_CACHE_KEY + userId;
        List<SysPermission> permissionList = cacheService.get(cacheKey);
        if (permissionList != null) {
            return permissionList;
        }
        
        // 缓存未命中，查询数据库
        permissionList = baseMapper.selectPermissionsByUserId(userId);
        
        // 存入缓存
        cacheService.set(cacheKey, permissionList, permissionCacheExpiration);
        
        return permissionList;
    }
}